package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserRealnameMapper;

import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.Objects;
import java.util.Random;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private IWemediaClient wemediaClient;

    @Autowired
    private ApUserService apUserService;

    @Override
    public ResponseResult list(AuthDto authDto) {

        //构建分页
        Page<ApUserRealname> page = new Page<>();
        page.setSize(authDto.getSize());
        page.setCurrent(authDto.getPage());
        //查询数据
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(authDto.getStatus())) {

            queryWrapper.eq(ApUserRealname::getStatus,authDto.getStatus());
        }
        queryWrapper.orderByDesc(ApUserRealname::getUpdatedTime);
        page = this.page(page, queryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(page);
        pageResponseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        pageResponseResult.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());

        return pageResponseResult;
    }

    @Override
    public ResponseResult authFail(AuthDto authDto) {
        ApUserRealname userRealname = this.getById(authDto.getId());
        userRealname.setUpdatedTime(new Date());
        userRealname.setReason(authDto.getMsg());
        userRealname.setStatus(2);
        this.updateById(userRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult authPass(AuthDto authDto) {

        ApUserRealname userRealname = this.getById(authDto.getId());
        userRealname.setUpdatedTime(new Date());
        userRealname.setReason(authDto.getMsg());
        userRealname.setStatus(9);
        this.updateById(userRealname);

        //从ap_user取出数据,修改用户为自媒体用户
        ApUser user = apUserService.getById(userRealname.getUserId());
        user.setIsIdentityAuthentication(1);
        user.setFlag(1);
        apUserService.updateById(user);


        WmUser wmUser = new WmUser();
        wmUser.setApUserId(user.getId());
        wmUser.setName(userRealname.getName());
        wmUser.setPassword(user.getPassword());
        wmUser.setSalt(user.getSalt());
        wmUser.setImage(user.getImage());
        wmUser.setPhone(user.getPhone());
        wmUser.setStatus(9);
        wmUser.setLoginTime(new Date());
        wmUser.setCreatedTime(new Date());

        wemediaClient.save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
